const {database} = require("@common/config")

class FoodService{
  async searchFoodClassify(id){
    try {
      const statement = `SELECT * FROM wx_food_classify`
      const [result] = await database.promise().execute(statement)
      return result
    } catch (err) {
      throw new Error(err)
    }
  }

  async searchAllFoodShop(pageIndex,pageSize){
    try {
      const statement = `SELECT * FROM wx_food_shop LIMIT ?,?`
      const [result] = await database.promise().execute(statement,[String(pageIndex),String(pageSize)])
      return result
    } catch (error) {
      throw new Error(err)
    }
  }

  async getAllFoodShopTotalSize(){
    try {
      const statement = `SELECT COUNT(*) as totalSize from wx_food_shop`
      const [[result]] = await database.promise().execute(statement)
      return result
    } catch (err) {
      throw new Error(err)
    }
  }

  async searchFoodShop(keyname){
    try {
      const statement = `SELECT * FROM wx_food_shop where name like ? `
      const result = await database.promise().execute(statement,[`%${keyname}%`])
      const sql = `SELECT * FROM wx_food_dishes where name like ?`
      const res = await database.promise().execute(sql,[`%${keyname}%`])
      return {
        dishes:res?.[0],
        shop:result?.[0]
      }
    } catch (err) {
      throw new Error(err)
    }
  }

  async getShopById(shopId){
    try {
      const statement = `SELECT * FROM wx_food_shop where id = ? `
      const result = await database.promise().execute(statement,[shopId])
      return result?.[0]
    } catch (err) {
      console.log(err);
      throw new Error(err)
    }
  }

  async getDishesById(shopId){
    try {
      const statement = `SELECT 
      t1.id,t1.name,t1.shop_id,t1.image,t1.price,t1.limited,t1.is_limited,t1.original_price,t1.num,t2.classify_name,t2.classify_name_id
      FROM wx_food_dishes t1 
      JOIN wx_dish_classify t2 ON t1.classify_id = t2.classify_name_id
      where t1.shop_id = ? `
      const result = await database.promise().execute(statement,[shopId])
      return result?.[0]
    } catch (err) {
      console.log(err);
      throw new Error(err)
    }
  }

  async getDiscountDishes(){ //写点假数据
    try {
      const statement = `SELECT t.id,t.name,t.shop_id,t.image,t.classify_id,t.price,t.original_price FROM wx_food_dishes t INNER JOIN wx_food_shop t1 ON t.shop_id = t1.id where t1.is_receive_order = 1`;
      const result = await database.promise().execute(statement)
      return result?.[0] || []
    } catch (err) {
      console.log(err);
      throw new Error(err)
    }
  }

  async filterShop(condition,pageIndex ,pageSize){
    try {
      const statement = `SELECT * FROM wx_food_shop t ORDER BY t.is_receive_order,${condition} LIMIT ?,?`
      const result = await database.promise().execute(statement,[pageIndex + '',pageSize + ''])
      return result?.[0]
    } catch (err) {
      console.log(err);
      throw new Error(err)
    }
  }

  async getShopCommentById(shopId,pageIndex ,pageSize){
    try {
      const statement = `
      SELECT
        t.id,
        t.comment_word,
        t.shop_id,
        t.images,
        t.rate,
        t.createAt,
        JSON_OBJECT(
          'id',
          u.id,
          'phone',
          u.phone,
          'createAt',
          u.createAt,
          'avatar',
          u.avatar,
          'updateAt',
          u.updateAt 
        ) AS userInfo 
      FROM
        wx_food_comment t
        INNER JOIN wx_user u ON t.user_id = u.id 
      WHERE
        t.shop_id = ? 
        LIMIT ?,?`;
      const result = await database.promise().execute(statement,[shopId,pageIndex + '' ,pageSize + ''])
      return result?.[0] || []
    } catch (err) {
      console.log(err);
      throw new Error(err)
    }
  }

  async getShopCommentTotalSize(shopId){
    let msql = `SELECT COUNT(*) as totalSize FROM wx_food_comment  where shop_id = ? `
    const [[totalSize]] = await database.promise().execute(msql,[shopId])
    return totalSize
  }

  async getShopByClassifId(classifyId){
    try {
      const statement = `SELECT * FROM wx_food_shop where classify_id = ?`
      const result = await database.promise().execute(statement,[String(classifyId)])
      return result?.[0]
    } catch (err) {
      console.log(err);
      throw new Error(err)
    }
  }
}

module.exports = new FoodService()